{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": true,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "#设置行不限制数量\n",
    "pd.set_option('display.max_rows',None)\n",
    "#设置列不限制数量\n",
    "pd.set_option('display.max_columns',None)\n",
    "#设置value的显示长度为100，默认为50\n",
    "pd.set_option('max_colwidth',100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"原始数据合并\"\"\"\n",
    "\n",
    "data_path = \"./data/origin_data/\"\n",
    "file_list = os.listdir(data_path)  # 原始数据文件列表\n",
    "data_list = []                     # 存储每个文件处理后的dataframe数据\n",
    "\n",
    "# 依次处理每个数据文件\n",
    "for file in file_list:\n",
    "    file_path = os.path.join(data_path, file)\n",
    "    df = pd.read_csv(file_path, index_col=None)\n",
    "    df.replace([np.inf, -np.inf, \"Infinity\", \"NaN\"], np.nan, inplace=True)   # 将无穷值替换为缺失值\n",
    "    df.dropna(axis=\"rows\", how=\"any\", inplace=True)                          # 删除缺失值所在行\n",
    "    data_list.append(df)\n",
    "\n",
    "frame = pd.concat(data_list, axis=0, ignore_index=True)   # 数据合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Destination Port</th>\n",
       "      <th>Flow Duration</th>\n",
       "      <th>Total Fwd Packets</th>\n",
       "      <th>Total Backward Packets</th>\n",
       "      <th>Total Length of Fwd Packets</th>\n",
       "      <th>Total Length of Bwd Packets</th>\n",
       "      <th>Fwd Packet Length Max</th>\n",
       "      <th>Fwd Packet Length Min</th>\n",
       "      <th>Fwd Packet Length Mean</th>\n",
       "      <th>Fwd Packet Length Std</th>\n",
       "      <th>Bwd Packet Length Max</th>\n",
       "      <th>Bwd Packet Length Min</th>\n",
       "      <th>Bwd Packet Length Mean</th>\n",
       "      <th>Bwd Packet Length Std</th>\n",
       "      <th>Flow Bytes/s</th>\n",
       "      <th>Flow Packets/s</th>\n",
       "      <th>Flow IAT Mean</th>\n",
       "      <th>Flow IAT Std</th>\n",
       "      <th>Flow IAT Max</th>\n",
       "      <th>Flow IAT Min</th>\n",
       "      <th>Fwd IAT Total</th>\n",
       "      <th>Fwd IAT Mean</th>\n",
       "      <th>Fwd IAT Std</th>\n",
       "      <th>Fwd IAT Max</th>\n",
       "      <th>Fwd IAT Min</th>\n",
       "      <th>Bwd IAT Total</th>\n",
       "      <th>Bwd IAT Mean</th>\n",
       "      <th>Bwd IAT Std</th>\n",
       "      <th>Bwd IAT Max</th>\n",
       "      <th>Bwd IAT Min</th>\n",
       "      <th>Fwd PSH Flags</th>\n",
       "      <th>Bwd PSH Flags</th>\n",
       "      <th>Fwd URG Flags</th>\n",
       "      <th>Bwd URG Flags</th>\n",
       "      <th>Fwd Header Length</th>\n",
       "      <th>Bwd Header Length</th>\n",
       "      <th>Fwd Packets/s</th>\n",
       "      <th>Bwd Packets/s</th>\n",
       "      <th>Min Packet Length</th>\n",
       "      <th>Max Packet Length</th>\n",
       "      <th>Packet Length Mean</th>\n",
       "      <th>Packet Length Std</th>\n",
       "      <th>Packet Length Variance</th>\n",
       "      <th>FIN Flag Count</th>\n",
       "      <th>SYN Flag Count</th>\n",
       "      <th>RST Flag Count</th>\n",
       "      <th>PSH Flag Count</th>\n",
       "      <th>ACK Flag Count</th>\n",
       "      <th>URG Flag Count</th>\n",
       "      <th>CWE Flag Count</th>\n",
       "      <th>ECE Flag Count</th>\n",
       "      <th>Down/Up Ratio</th>\n",
       "      <th>Average Packet Size</th>\n",
       "      <th>Avg Fwd Segment Size</th>\n",
       "      <th>Avg Bwd Segment Size</th>\n",
       "      <th>Fwd Header Length.1</th>\n",
       "      <th>Fwd Avg Bytes/Bulk</th>\n",
       "      <th>Fwd Avg Packets/Bulk</th>\n",
       "      <th>Fwd Avg Bulk Rate</th>\n",
       "      <th>Bwd Avg Bytes/Bulk</th>\n",
       "      <th>Bwd Avg Packets/Bulk</th>\n",
       "      <th>Bwd Avg Bulk Rate</th>\n",
       "      <th>Subflow Fwd Packets</th>\n",
       "      <th>Subflow Fwd Bytes</th>\n",
       "      <th>Subflow Bwd Packets</th>\n",
       "      <th>Subflow Bwd Bytes</th>\n",
       "      <th>Init_Win_bytes_forward</th>\n",
       "      <th>Init_Win_bytes_backward</th>\n",
       "      <th>act_data_pkt_fwd</th>\n",
       "      <th>min_seg_size_forward</th>\n",
       "      <th>Active Mean</th>\n",
       "      <th>Active Std</th>\n",
       "      <th>Active Max</th>\n",
       "      <th>Active Min</th>\n",
       "      <th>Idle Mean</th>\n",
       "      <th>Idle Std</th>\n",
       "      <th>Idle Max</th>\n",
       "      <th>Idle Min</th>\n",
       "      <th>Label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>54865</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.000000e+06</td>\n",
       "      <td>666666.66670</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>666666.666700</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>33</td>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>BENIGN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>55054</td>\n",
       "      <td>109</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.100917e+05</td>\n",
       "      <td>18348.62385</td>\n",
       "      <td>109.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>109</td>\n",
       "      <td>109</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>9174.311927</td>\n",
       "      <td>9174.311927</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>29</td>\n",
       "      <td>256</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>BENIGN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>55055</td>\n",
       "      <td>52</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.307692e+05</td>\n",
       "      <td>38461.53846</td>\n",
       "      <td>52.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>52</td>\n",
       "      <td>52</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>19230.769230</td>\n",
       "      <td>19230.769230</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>29</td>\n",
       "      <td>256</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>BENIGN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Destination Port   Flow Duration   Total Fwd Packets  \\\n",
       "0              54865               3                   2   \n",
       "1              55054             109                   1   \n",
       "2              55055              52                   1   \n",
       "\n",
       "    Total Backward Packets  Total Length of Fwd Packets  \\\n",
       "0                        0                           12   \n",
       "1                        1                            6   \n",
       "2                        1                            6   \n",
       "\n",
       "    Total Length of Bwd Packets   Fwd Packet Length Max  \\\n",
       "0                             0                       6   \n",
       "1                             6                       6   \n",
       "2                             6                       6   \n",
       "\n",
       "    Fwd Packet Length Min   Fwd Packet Length Mean   Fwd Packet Length Std  \\\n",
       "0                       6                      6.0                     0.0   \n",
       "1                       6                      6.0                     0.0   \n",
       "2                       6                      6.0                     0.0   \n",
       "\n",
       "   Bwd Packet Length Max   Bwd Packet Length Min   Bwd Packet Length Mean  \\\n",
       "0                      0                       0                      0.0   \n",
       "1                      6                       6                      6.0   \n",
       "2                      6                       6                      6.0   \n",
       "\n",
       "    Bwd Packet Length Std  Flow Bytes/s   Flow Packets/s   Flow IAT Mean  \\\n",
       "0                     0.0  4.000000e+06     666666.66670             3.0   \n",
       "1                     0.0  1.100917e+05      18348.62385           109.0   \n",
       "2                     0.0  2.307692e+05      38461.53846            52.0   \n",
       "\n",
       "    Flow IAT Std   Flow IAT Max   Flow IAT Min  Fwd IAT Total   Fwd IAT Mean  \\\n",
       "0            0.0              3              3              3            3.0   \n",
       "1            0.0            109            109              0            0.0   \n",
       "2            0.0             52             52              0            0.0   \n",
       "\n",
       "    Fwd IAT Std   Fwd IAT Max   Fwd IAT Min  Bwd IAT Total   Bwd IAT Mean  \\\n",
       "0           0.0             3             3              0            0.0   \n",
       "1           0.0             0             0              0            0.0   \n",
       "2           0.0             0             0              0            0.0   \n",
       "\n",
       "    Bwd IAT Std   Bwd IAT Max   Bwd IAT Min  Fwd PSH Flags   Bwd PSH Flags  \\\n",
       "0           0.0             0             0              0               0   \n",
       "1           0.0             0             0              0               0   \n",
       "2           0.0             0             0              0               0   \n",
       "\n",
       "    Fwd URG Flags   Bwd URG Flags   Fwd Header Length   Bwd Header Length  \\\n",
       "0               0               0                  40                   0   \n",
       "1               0               0                  20                  20   \n",
       "2               0               0                  20                  20   \n",
       "\n",
       "   Fwd Packets/s   Bwd Packets/s   Min Packet Length   Max Packet Length  \\\n",
       "0  666666.666700        0.000000                   6                   6   \n",
       "1    9174.311927     9174.311927                   6                   6   \n",
       "2   19230.769230    19230.769230                   6                   6   \n",
       "\n",
       "    Packet Length Mean   Packet Length Std   Packet Length Variance  \\\n",
       "0                  6.0                 0.0                      0.0   \n",
       "1                  6.0                 0.0                      0.0   \n",
       "2                  6.0                 0.0                      0.0   \n",
       "\n",
       "   FIN Flag Count   SYN Flag Count   RST Flag Count   PSH Flag Count  \\\n",
       "0               0                0                0                0   \n",
       "1               0                0                0                0   \n",
       "2               0                0                0                0   \n",
       "\n",
       "    ACK Flag Count   URG Flag Count   CWE Flag Count   ECE Flag Count  \\\n",
       "0                1                0                0                0   \n",
       "1                1                1                0                0   \n",
       "2                1                1                0                0   \n",
       "\n",
       "    Down/Up Ratio   Average Packet Size   Avg Fwd Segment Size  \\\n",
       "0               0                   9.0                    6.0   \n",
       "1               1                   9.0                    6.0   \n",
       "2               1                   9.0                    6.0   \n",
       "\n",
       "    Avg Bwd Segment Size   Fwd Header Length.1  Fwd Avg Bytes/Bulk  \\\n",
       "0                    0.0                    40                   0   \n",
       "1                    6.0                    20                   0   \n",
       "2                    6.0                    20                   0   \n",
       "\n",
       "    Fwd Avg Packets/Bulk   Fwd Avg Bulk Rate   Bwd Avg Bytes/Bulk  \\\n",
       "0                      0                   0                    0   \n",
       "1                      0                   0                    0   \n",
       "2                      0                   0                    0   \n",
       "\n",
       "    Bwd Avg Packets/Bulk  Bwd Avg Bulk Rate  Subflow Fwd Packets  \\\n",
       "0                      0                  0                    2   \n",
       "1                      0                  0                    1   \n",
       "2                      0                  0                    1   \n",
       "\n",
       "    Subflow Fwd Bytes   Subflow Bwd Packets   Subflow Bwd Bytes  \\\n",
       "0                  12                     0                   0   \n",
       "1                   6                     1                   6   \n",
       "2                   6                     1                   6   \n",
       "\n",
       "   Init_Win_bytes_forward   Init_Win_bytes_backward   act_data_pkt_fwd  \\\n",
       "0                      33                        -1                  1   \n",
       "1                      29                       256                  0   \n",
       "2                      29                       256                  0   \n",
       "\n",
       "    min_seg_size_forward  Active Mean   Active Std   Active Max   Active Min  \\\n",
       "0                     20          0.0          0.0            0            0   \n",
       "1                     20          0.0          0.0            0            0   \n",
       "2                     20          0.0          0.0            0            0   \n",
       "\n",
       "   Idle Mean   Idle Std   Idle Max   Idle Min   Label  \n",
       "0        0.0        0.0          0          0  BENIGN  \n",
       "1        0.0        0.0          0          0  BENIGN  \n",
       "2        0.0        0.0          0          0  BENIGN  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "pycharm": {
     "is_executing": true,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BENIGN                        1741839\n",
       "DoS Hulk                       230124\n",
       "PortScan                       158804\n",
       "DDoS                           128025\n",
       "DoS GoldenEye                   10293\n",
       "FTP-Patator                      7935\n",
       "SSH-Patator                      5897\n",
       "DoS slowloris                    5796\n",
       "DoS Slowhttptest                 5499\n",
       "Bot                              1956\n",
       "Web Attack � Brute Force         1507\n",
       "Web Attack � XSS                  652\n",
       "Infiltration                       36\n",
       "Web Attack � Sql Injection         21\n",
       "Heartbleed                         11\n",
       "Name:  Label, dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame[\" Label\"].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "可以发现，数据集存在严重的的样本不均衡问题，正常流量（BENIGN）在数据集中的占比很大，而其他一些异常流量则占比很小，这就容易导致训练出的模型更偏向于将输入样本预测为占比大的那一类，实际应用场景中并不能达到我们预期的效果。为此我们对数据集进行平衡样本处理，仅选择前六个类别进行训练预测。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"平衡数据集样本\"\"\"\n",
    "BENIGN = frame[frame[' Label']=='BENIGN'][:250000]\n",
    "DosHulk = frame[frame[' Label']=='DoS Hulk']\n",
    "PortScan = frame[frame[' Label']=='PortScan']\n",
    "DDoS = frame[frame[' Label']=='DDoS']\n",
    "DoSGoldenEye = frame[frame[' Label']=='DoS GoldenEye']\n",
    "FTPPatator = frame[frame[' Label']=='FTP-Patator']\n",
    "\n",
    "new_dataset = pd.concat([BENIGN, DosHulk, PortScan, DDos, DoSGoldenEye, FTPPatator])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 选出11个典型特征进行训练\n",
    "final_columns = [' Bwd Packet Length Min',' Subflow Fwd Bytes','Total Length of Fwd Packets',\n",
    "                 ' Fwd Packet Length Mean',' Bwd Packet Length Std',' Flow Duration',' Flow IAT Std',\n",
    "                 'Init_Win_bytes_forward',' Bwd Packets/s', ' PSH Flag Count',' Average Packet Size',' Label' ]\n",
    "dataset = new_dataset[final_columns]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/ipykernel/__main__.py:8: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "\"\"\"特征编码\"\"\"\n",
    "# 将类别特征转换为离散性数值\n",
    "y = dataset[' Label']                      # 经过对数据的进一步观察，发现只有Label列为类别特征\n",
    "le = LabelEncoder()                         # 编码器实例化\n",
    "le = le.fit(y)                              # 导入数据\n",
    "label = le.transform(y)                     # 接口调取结果\n",
    "dataset[' Label'] = label                 # 更新对应属性列的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    250000\n",
       "2    230124\n",
       "4    158804\n",
       "1     10293\n",
       "3      7935\n",
       "Name:  Label, dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看编码后的Label属性列\n",
    "dataset[\" Label\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['BENIGN', 'DoS GoldenEye', 'DoS Hulk', 'FTP-Patator', 'PortScan'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 编码前后的对应关系\n",
    "match = le.inverse_transform([0, 1, 2, 3, 4])\n",
    "match"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 保存清洗过的数据为csv文件\n",
    "dataset.to_csv('./data/processed_data/dataset6classes.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:MindSpore] *",
   "language": "python",
   "name": "conda-env-MindSpore-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
